/*	$Id: string.c 1099 2010-11-27 16:55:00Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#include "log.h"
#include "mem.h"
#include "string.h"
#include "private.h"

/*
 * cnp_strdup()
 *
 * Allocate a new string that is a copy of the string pointed to by <string>.
 * This function is equivalent to strdup(3), and is provided because strdup()
 * is not part of any standard.
 * Returns a pointer to the allocated string on success, or NULL on failure.
 */
char*
cnp_strdup(const char *string)
{
	size_t len;
	char *copy;

	len = strlen(string) + 1;

	if ((copy = cnp_malloc(len)) != NULL)
		strlcpy(copy, string, len);

	return (copy);
}

/*
 * cnp_strndup()
 *
 * Allocate a new string and set its contents to the first <len> bytes of the
 * string pointed to by <string>.
 * Returns a pointer to the allocated string on success, or NULL on failure.
 */
char*
cnp_strndup(const char *string, size_t len)
{
	size_t l;
	char *copy;

	if ((l = strlen(string)) > len)
		l = len;

	if ((copy = cnp_malloc(l + 1)) != NULL)
		strlcpy(copy, string, l + 1);

	return (copy);
}

/*
 * cnp_strnrchr()
 *
 * Look for the last occurrence of the character <c> within the first <len>
 * bytes of the string <string>.
 *
 * Returns a pointer to the location of the last instance of <c> if found,
 * or NULL otherwise.
 */
char*
cnp_strnrchr(const char *string, size_t len, int c)
{
	size_t l;
	const char *cp;

	l = strlen(string);

	cp = string + ((l > len) ? len : l) - 1;
	while (cp != string) {
		if (*cp == c)
			return (char *)(cp);
		cp--;
	}

	return (NULL);
}

/*
 * cnp_asprintf()
 *
 *
 * Returns the length of the string that was allocated on success, or -1 if
 * an error occurred.
 */
int
cnp_asprintf(char **dst, const char *fmt, ...)
{
	int ret;
	va_list vap;

	va_start(vap, fmt);
	ret = cnp_vasprintf(dst, fmt, vap);
	va_end(vap);

	return (ret);
}

/*
 * cnp_vasprintf()
 *
 * Returns the length of the string that was allocated on success, or -1 if
 * an error occurred.
 */
int
cnp_vasprintf(char **dst, const char *fmt, va_list vap)
{
	int ret;
	size_t len = 128;
	char *buf;

	if ((buf = cnp_malloc(len)) == NULL)
		return (-1);

	do {
		ret = vsnprintf(buf, len, fmt, vap);
		if (ret >= 0) {
			buf = cnp_realloc(buf, (size_t)(ret + 1));
			if (buf == NULL)
				return (-1);
			if (ret < (int)len)
				break;
			len = (size_t)ret + 1;
		}
	} while (1);

	*dst = buf;

	return (ret);
}
